# -*- coding: utf-8 -*- 
"""
@Author :
@Date : 2022/7/19 10:21
File Description:

"""
import time
import sys
import json
import psutil
import requests
import vthread
import numpy as np
import pandas as pd
from collections import Counter
from datetime import datetime
from clickhouse_driver import Client

from datetime import datetime, timedelta
from flask import jsonify, request, Blueprint, current_app

recogMobileFinal = Blueprint('recogMobileFinal', __name__)
interval_15 = 1296000
limit = 1000

# 计算线程数量
pc_mem = psutil.virtual_memory()
div_gb_factor = (1024 ** 2)
thread_n = int((pc_mem.available / div_gb_factor) // 200)
# 用于后台执行处理
pool0 = vthread.pool(10, gqueue=0)  # 开40个伺服线程，组名为0


@pool0
def run(date, config_ck ):
    print(config_ck)
    update_datas = []
    # define clickhouse connect
    client = Client(
        host=config_ck['host'],
        database=config_ck['database'],
        user=config_ck['user'],
        password=config_ck['password'])
    print(f"Begin of the ({date})")
    date_stamp = int(time.mktime(time.strptime(date, "%Y-%m-%d")))

    sql = f"SELECT  active_id, mobile_mac, toString(analysis_datetime), toUnixTimestamp(CONCAT(CAST(analysis_datetime as String), ' 00:00:00')) as ut " \
          f"FROM mobile_mac_day1 " \
          f"where ut >= {date_stamp - interval_15} and ut <= {date_stamp}"
    print(sql)
    data = client.execute(sql)

    create_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    analysis_datetime = datetime.now().strftime('%Y-%m-%d')
    if len(data) <= 0:
        return
    df_data = pd.DataFrame(data, columns=['active_id', 'mobile_mac', 'analysis_datetime', 'ut'])
    df_data = df_data.groupby(by=['active_id']).apply(lambda x: ','.join(x["mobile_mac"]))
    # print(df_data)
    rows = []
    for active_id, mobile_mac in df_data.items():
        countRes = Counter(mobile_mac.split(','))
        active_id_mobile_mac = {}
        mobile_mac_str = []
        for k, v in countRes.items():
            if v >= 3:
                mobile_mac_str.append(k)
            else:
                break
        if len(mobile_mac_str) > 0:
            active_id_mobile_mac["active_id"] = active_id
            active_id_mobile_mac["mobile_mac"] = ",".join(mobile_mac_str)
            active_id_mobile_mac["analysis_datetime"] = analysis_datetime
            active_id_mobile_mac["create_time"] = create_time
            rows.append(active_id_mobile_mac)

    sql_base = """insert into {table} FORMAT JSONEachRow {data_json}"""
    n = len(rows)
    if n > 0:
        for i in range(0, n, limit):
            sql = sql_base.format(table="mobile_mac_day15",
                                  data_json=json.dumps(rows[i:i + limit]).replace("]", "").replace("[", ""))
            client.execute(sql)
    client.disconnect()


@recogMobileFinal.route('/final', methods=['GET', 'POST'])
def final():
    """
    http://localhost:8964/recogMobileFinal/final?date=2022-07-01
    """
    # （merge 15d） clickhouse configures
    config_ck = {
        'host': current_app.config['CLICKHOUSE_HOST'],
        'port': current_app.config['CLICKHOUSE_PORT'],
        'user': current_app.config['CLICKHOUSE_USER'],
        'password': current_app.config['CLICKHOUSE_PASSWORD'],
        'database': current_app.config['CLICKHOUSE_DATABASE'],
       }

    register_dict = request.args
    date = register_dict['date']
    if validate(date):
        return jsonify({"status": "Incorrect data format, should be YYYY-MM-DD", "code": 1})
    run(date, config_ck)
    return jsonify({"code": 0})


def validate(date_text):
    try:
        datetime.strptime(date_text, '%Y-%m-%d')
        return 0
    except ValueError:
        return 1
